<?php 
require_once "./../../mods/include/dbfunctions.inc.php";
require_once "./../../mods/include/db.inc.php";

//mods.wottools.com/files/MSA/MSAVersion.php?userid=1&lang=test

$userid = -1;
if(isset($_GET["userid"]))
{
    $userid=$_GET["userid"];
}
$lang = "error";
if(isset($_GET["lang"]))
{
    $lang=$_GET["lang"];
}
$ip="unknown";
if(isset($_SERVER['REMOTE_ADDR']))
{
    $ip = $_SERVER['REMOTE_ADDR'];
}

logMSAVRequest($db,$userid,$lang,$ip);



$outarr = array();
$outarr["version"] = "1.1.5";

function buildpath($dir)
{
    $dirarr = explode ("/", $dir);
    unset ($dirarr[1]);
    unset ($dirarr[0]);
    $dir = implode ("/", $dirarr);
    return "/".$dir;
}

function getpath ($str)
{
    return "http://".implode ("/", array_slice (explode("/", realpath($str)), count(explode ("/",$_SERVER[ 'DOCUMENT_ROOT' ]))-1));
}

function processfiles($dir)
{
    $listDir = array();
    if($handler = opendir($dir)) {
        while (($sub = readdir($handler)) !== FALSE) {
            if ($sub != "." && $sub != ".." && $sub != "Thumb.db") {
                if(is_file($dir."/".$sub)) {
                    $path = buildpath ("$dir/$sub");
                    $listDir["$path"]["md5"] = md5_file ("$dir/$sub");
                    $listDir["$path"]["location"] = getpath("$dir/$sub");
                }elseif(is_dir($dir."/".$sub)){
                    $listDir2 = processfiles($dir."/".$sub);
                    foreach ($listDir2 as $key=>$value)
                    {
                        $listDir[$key]=$value; 
                    }
                }
            }
        }   
        closedir($handler);
    }
    return $listDir;   
}

function process(&$arr, $dir)
{
    $arr["filemap"]["$dir"] = processfiles ("./".$dir);
}


if($handler = opendir(".")) {
    while (($sub = readdir($handler)) !== FALSE) {
        if ($sub != "." && $sub != ".." && $sub != "Thumb.db" ) {
            if(is_dir("./".$sub)){
                process ($outarr, $sub);
            }
        }
    }   
    closedir($handler);
}
echo json_encode($outarr);

    
/*
{
    "version": "1.1.5",
    "filemap" : {
        "0.9.0" : {
            "/gui/flash/settingsWindow.swf" : {
                "md5" : "b34b1919265e2f5cacd25cfd827a0fb6",
                "location" : "http://mods.wottools.com/files/MSA/0.9.0/gui/flash/settingsWindow.swf"
            }
        "0.9.2: { 
            "/gui/flash/test.txt" : {
            "md5" : "",
            "location" : "http://mods.wottools.com/files/MSA/0.9.2/gui/flash/test.txt"
            }
        
        }
    },
    "stale" : 
        {
        }
    }
}
*/
?>